package com.program.beautymakeup.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.program.beautymakeup.domain.bo.PortfolioQueryBO;
import com.program.beautymakeup.domain.bo.PortfolioSaveBO;
import com.program.beautymakeup.domain.entity.Portfolio;
import com.program.beautymakeup.domain.entity.PortfolioImg;
import com.program.beautymakeup.domain.entity.User;
import com.program.beautymakeup.domain.vo.PageVO;
import com.program.beautymakeup.domain.vo.PortfolioRsp;
import com.program.beautymakeup.domain.vo.ResultInfo;
import com.program.beautymakeup.domain.vo.UserVO;
import com.program.beautymakeup.mapper.PortfolioImgMapper;
import com.program.beautymakeup.mapper.PortfolioMapper;
import com.program.beautymakeup.mapper.UserMapper;
import com.program.beautymakeup.utils.ResultInfoUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.sound.sampled.Port;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author bytedance
 * @ClassName PortfolioService.java
 * @Description TODO
 * @createTime 2023年02月09日 22:26:00
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PortfolioService {

    @Autowired
    private PortfolioMapper portfolioMapper;
    @Autowired
    private PortfolioImgMapper portfolioImgMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UploadService uploadService;

    public PageVO<PortfolioRsp> queryList(PortfolioQueryBO portfolioQueryBO) {
        QueryWrapper<Portfolio> queryWrapper = new QueryWrapper<>();
        if (portfolioQueryBO.getUserId() != null) queryWrapper.eq("user_id", portfolioQueryBO.getUserId());
        if (portfolioQueryBO.getId() != null) queryWrapper.eq("id", portfolioQueryBO.getId());
        if (portfolioQueryBO.getType() != null) queryWrapper.eq("type", portfolioQueryBO.getType());
        if (portfolioQueryBO.getName() != null) queryWrapper.likeRight("name", portfolioQueryBO.getName());
        IPage<Portfolio> iPage = portfolioMapper.selectPage(new Page<>(portfolioQueryBO.getCurrent(), portfolioQueryBO.getPageSize()), queryWrapper);
        PageVO<PortfolioRsp> pageVO = new PageVO<>();
        pageVO.build(iPage);
        pageVO.setRecords(findPortfolioRsp(iPage.getRecords()));
        return pageVO;
    }

    public List<PortfolioRsp> findPortfolioRsp(List<Portfolio> portfolioList) {
        List<PortfolioRsp> ans = new ArrayList<>();
        for (Portfolio portfolio : portfolioList) {
            PortfolioRsp item = BeanUtil.copyProperties(portfolio, PortfolioRsp.class);
            item.setPortfolioImgList(portfolioImgMapper.selectByPortfolioId(portfolio.getId()));
            // 查询用户姓名
            User user = userMapper.selectById(portfolio.getUserId());
            if (user != null) item.setUsername(user.getUsername());
            ans.add(item);
        }
        return ans;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public boolean delete(Integer portfolioId) {
        final Portfolio portfolio = portfolioMapper.selectByPrimaryKey(portfolioId);
        if (portfolio == null) {
            return false;
        }
        final QueryWrapper<PortfolioImg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("portfolio_id", portfolioId);
        List<PortfolioImg> portfolioImgList = portfolioImgMapper.selectList(queryWrapper);
        uploadService.deleteFile(portfolioImgList.stream().map(PortfolioImg::getUrl).collect(Collectors.toList()));
        List<Integer> ids = portfolioImgList.stream().map(PortfolioImg::getId).collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(ids)) portfolioImgMapper.deleteBatchIds(ids);
        int row = portfolioMapper.deleteById(portfolioId);
        return row > 0;
    }

}
